package com.hawk.eye.server.convert;

import com.hawk.eye.server.domain.dto.req.GroupReqDTO;
import com.hawk.eye.server.domain.dto.resp.GroupRespDTO;
import com.hawk.eye.server.domain.dto.resp.TreeRespDTO;
import com.hawk.eye.server.domain.po.Group;
import com.hawk.eye.server.domain.vo.req.GroupReqVO;
import com.hawk.eye.server.domain.vo.resp.GroupRespVO;
import com.hawk.eye.server.domain.vo.resp.TreeRespVO;
import org.mapstruct.DecoratedWith;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zd
 */
@Mapper
@DecoratedWith(GroupConvert.GroupConvertDecorator.class)
public interface GroupConvert {

    GroupConvert INSTANCE = Mappers.getMapper(GroupConvert.class);

    GroupRespDTO poToRespDto(Group group);

    @IterableMapping(qualifiedByName = "poToRespVo")
    List<GroupRespVO> poToRespVoList(List<Group> group);

    @Named(value = "poToRespVo")
    GroupRespVO poToRespVo(Group group);

    List<GroupRespDTO> poToRespDtoList(List<Group> groups);

    @IterableMapping(qualifiedByName = "resDtoToRespVo")
    List<GroupRespVO> resDtoToRespVoList(List<GroupRespDTO> dtoList);

    @Named(value = "resDtoToRespVo")
    GroupRespVO resDtoToRespVo(GroupRespDTO dto);

    GroupReqDTO reqVoToReqDto(GroupReqVO reqVO);
    
    Group reqDtoToPo(GroupReqDTO reqDTO);

    @IterableMapping(qualifiedByName = "dtoTreeToVoTree")
    List<TreeRespVO> dtoTreeToVoTreeList(List<TreeRespDTO> tree);

    @Named(value = "dtoTreeToVoTree")
    TreeRespVO dtoTreeToVoTree(TreeRespDTO dto);

    public abstract class GroupConvertDecorator implements GroupConvert{


        @Override
        public TreeRespVO dtoTreeToVoTree(TreeRespDTO dto) {
            if (dto == null){
                return null;
            }
            TreeRespVO vo = new TreeRespVO();
            vo.setId(dto.getId());
            vo.setName(dto.getName());
            vo.setType(dto.getType());
            if (dto.getChildren() != null){
                List<TreeRespVO> children = new ArrayList<>();
                for (TreeRespDTO child : dto.getChildren()) {
                    children.add(dtoTreeToVoTree(child));
                }
                vo.setChildren(children);
            }
            return vo;
        }

    }

}